<template>
	<view class="u-wrap">
		<view class="u-search-box">
			<view class="u-search-inner">
				<u-icon name="search" color="#909399" :size="28"></u-icon>
				<text class="u-search-text">搜索产品</text>
			</view>
		</view>
		<view class="tabs-display-area">
			<u-tabs :list="list" :is-scroll="false" :bold="false" :current="tabsCurrent" @change="tabsChange" bg-color="#1db0fc" inactive-color="#fff" active-color="#ffffff"></u-tabs>
		</view>
		<view class="u-menu-wrap">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<view v-for="(item,index) in tabbar" :key="index" class="u-tab-item"
					:class="[current==index ? 'u-tab-item-active' : '']" :data-current="index"
					@tap.stop="swichMenu(index)">
					<text class="u-line-1">{{item.name}}</text>
				</view>
			</scroll-view>
			<block v-for="(item,index) in tabbar" :key="index">
				<scroll-view scroll-y class="right-box" v-show="current==index">
					<view class="page-view">
						<view class="class-item">
							<view class="item-container">
								<view class="thumb-box bg-white" v-for="(item1, index1) in item.goods" :key="index1">
									<view class="product-information">
										<image class="item-menu-image" :src="item1.image" mode="widthFix"></image>
										<view class="">
											<view class="item-menu-name">{{item1.name}}</view>
											<view class="item-price">￥<text class="price">{{item1.price}}</text></view>
											<view class="item-sales-quantity">已售{{item1.sales_sum}}</view>
										</view>
									</view>
									<view class="operating-button-display-area">
										<u-row>
											<u-col class="item" span="4">
												<text class="title">编辑</text>
											</u-col>
											<u-col class="item" span="4">
												<text class="title">下架</text>
											</u-col>
											<u-col class="item" span="4">
												<text class="title">删除</text>
											</u-col>
										</u-row>
									</view>
								</view>

								<view v-if="item.goods == undefined || item.goods.length <= 0"
									style="position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);">
									<u-empty text="暂无商品" mode="list"></u-empty>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</block>
		</view>
		<view class="footer bg-white">
			<u-row>
				<u-col span="4" class="item" @click="goPage('/pages/bundle/merchants/goods/create')">
					<u-icon name="plus" color="#1db0fc" size="34"></u-icon>
					<text class="title">新建商品</text>
				</u-col>
				<u-col span="4" class="item" @click="goPage('/pages/bundle/merchants/goods_category/lists')">
					<u-icon name="grid-fill" color="#1db0fc" size="34"></u-icon>
					<text class="title">管理分类</text>
				</u-col>
				<u-col span="4" class="item">
					<u-icon name="bag-fill" color="#1db0fc" size="34"></u-icon>
					<text class="title">商品库</text>
				</u-col>
			</u-row>
		</view>
	</view>
</template>
<script>
	import classifyData from '@/common/classify.data.js';
	import {
		MerchantGoodsCategory,
		MerchantGetGoodsList
	} from '@/api/merchant';
	export default {
		data() {
			return {
				tabbar: [],
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				list: [{
					name: '出售中'
				}, {
					name: '未上架'
				}],
				tabsCurrent: 0
			}
		},
		onLoad() {
			var _this = this;
			MerchantGoodsCategory().then(res => {
				if (res.code == 1) {
					_this.tabbar = res.data;
					for (var key in _this.tabbar) {
						_this.tabbar[key].is_loader_goods = false;
						if (key == 0) {
							_this.swichMenu(0)
						}
					}
				}
			})
		},
		methods: {
			getImg() {
				return Math.floor(Math.random() * 35);
			},
			// 点击左边的栏目切换
			async swichMenu(index) {
				if (this.tabbar[index].is_loader_goods == false) {
					MerchantGetGoodsList({
						category_id: this.tabbar[index].id
					}).then(res => {
						if (res.code == 1) {
							this.tabbar[index].is_loader_goods = true;
							this.tabbar[index].goods = res.data;
							this.$forceUpdate()
						}
					})
				}

				if (index == this.current) return;
				this.current = index;
				// 如果为0，意味着尚未初始化
				if (this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({
						size: true
					}, res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			},
			goPage(url) {
				uni.navigateTo({
					url
				})
			},
			tabsChange(index) {
				this.tabsCurrent = index;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
	}

	.u-search-box {
		padding: 18rpx 30rpx;
		background-color: #1db0fc;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	.u-search-inner {
		background-color: #ffffff;
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 16rpx;
	}

	.u-search-text {
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}

	.u-tab-view {
		width: 200rpx;
		height: 100%;
	}

	.u-tab-item {
		height: 80rpx;
		background: #f6f6f6;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
	}

	.u-tab-item-active {
		position: relative;
		color: #000;
		font-size: 30rpx;
		font-weight: 600;
		background: #fff;
	}

	.u-tab-item-active::before {
		content: "";
		position: absolute;
		border-left: 4px solid #1db0fc;
		height: 32rpx;
		left: 0;
		top: 22rpx;
	}

	.u-tab-view {
		height: 100%;
	}

	.right-box {
		background-color: rgb(250, 250, 250);
	}

	.page-view {
		padding: 16rpx;
		background-color: #fff;
	}

	.class-item {
		padding-bottom: 30rpx;
		// background-color: #fff;
		// padding: 16rpx;
		border-radius: 8rpx;
		position: relative;
	}

	.class-item:last-child {
		min-height: 100vh;
	}

	.item-menu-name {
		font-size: 15px;
		font-weight: 700;
		color: #333;
	}

	.item-container {
		display: flex;
		flex-wrap: wrap;
	}

	.thumb-box {
		width: 100%;
		padding: 20rpx;
		margin-bottom: 20rpx;
		border: 1px solid rgba(144, 147, 153,.2);
		border-radius: 20rpx;
		background: #f6f6f6;

		.product-information {
			display: flex;

			.item-price {
				color: red;
				font-weight: bold;
				font-size: 22rpx;
				padding-top: 20rpx;

				.price {
					font-size: 32rpx;
				}
			}

			.item-sales-quantity {
				color: #999;
				font-size: 22rpx;
				margin-top: 16rpx;
			}
		}
	}

	.item-menu-image {
		width: 34.7449%;
		margin-right: 20rpx;
	}

	.operating-button-display-area {
		line-height: 30rpx;
		margin-top: 20rpx;
		border-top: 2rpx solid #ddd;

		.item {
			border-right: 2rpx solid #ddd;
			margin-top: 20rpx;
			margin-bottom: 10rpx;

			&:last-child {
				border-right: none;
			}

			.title {
				text-align: center;
				display: block;
				color: #1db0fc;
				font-weight: bold;
			}
		}
	}

	.footer {
		line-height: 50rpx;
		border-top: 2rpx solid #f6f6f6;

		.item {
			margin: 20rpx 0;
			text-align: center !important;
			border-right: 2rpx solid #ddd;

			&:last-child {
				border-right: none;
			}

			.title {
				padding-left: 10rpx;
			}
		}
	}
</style>
